// author: LucunJi
// 2020-07-06 00:03:44
//
// this plugin allows you to export a block to use in OpenComputer mod
// the format of exported .3dm files follows instructions here: https://github.com/OpenPrograms/Sangar-Programs/blob/master/models/example.3dm
// you need print3d.lua (https://github.com/OpenPrograms/Sangar-Programs/blob/master/print3d.lua) and a 3D printer in addition
//
// thanks to Gamepiaynmo's cpm_plugin(https://github.com/Gamepiaynmo/CustomModel/blob/master/plugin/cpm_model.js), I learned a lot from his code

(function() {
	let export_action;
	let options = {};

	Plugin.register('oc_3dm_model', {
		title: 'OC 3dm Model Format',
		icon: 'computer',
		author: 'Lucun_Ji',
		description: 'Export models of OpenComputer mod in .3dm format.',
		version: '0.1.0',
		variant: 'desktop',

		onload() {
			let codec = new Codec('oc_3dm_model', {
				name: 'OC .3dm Model',
				extension: '3dm',
				remember: false,

				compile() {
					function convertCube(cube) {
						let str = '    {';
						cube.from.forEach(i => {
							str += i + ', ';
						});
						cube.to.forEach(i => {
							str += i + ', ';
						});

						str += 'texture = "';
						for (let i = 0; i < textures.length; i++) {
							if (textures[i].uuid == cube.faces.north.texture) {
								str += textures[i].name.replace(/\.[^/.]+$/, "");
								break;
							}
						}
						str += '"';

						str += '}'
						return str;
					}

					function handleObj(obj) {
						let arr = [];
						if (obj instanceof Cube) {
							arr.push('    -- ' + obj.name + '\n');
							arr.push(convertCube(obj) + ',\n');
						} else if (obj instanceof Group) {
							arr.push('\n');
							arr.push('    -- ' + obj.name + '\n');
							for (let child of obj.children) {
								handleObj(child).forEach(i => arr.push('  ' + i));
							}
						}
						return arr;
					}

					let model = '-- generated by Blockbench, with OpenComputer .3dm Model plugin\n';
					model += '{\n';
					model += '  label = "' + options.label + '",\n';
					model += '  lightLevel = ' + options.lightLevel + ',\n';
					model += '  emitRedstone = ' + options.emitRedstone + ',\n';
					model += '  buttonMode = ' + options.buttonMode + ',\n';
					model += '  collidable = {' + options.collidableInactive + ', ' + options.collidableActive + '},\n';
					model += '\n';
					model += '  shapes = {\n';

					Outliner.root.forEach(obj => {
						handleObj(obj).forEach(i => model += i);
					})

					model += '  }\n';
					model += '}'

					return model;
				},
				parse(model, path) {

				}
			})

			export_action = new Action('export_oc_3dm_model', {
				name: 'Export OC .3dm model',
				description: '',
				icon: 'computer',
				category: 'file',
				click() {
					let dialog = new Dialog({
						title: 'OC .3dm Model Export',
						id: 'oc_3dm_export',
						lines: [
							'<p>Block Name: <input type="text" id="label" value="" style="background-color:var(--color-back)"></p>',
							'<p>Light Level(0~8): <input type="number" id="lightLevel" value=0 style="background-color:var(--color-back)"></p>',
							'<p>Redstone Signal Level(0~15): <input type="number" id="emitRedstone" value=0 style="background-color:var(--color-back)"></p>',
							'<p>Button Mode: <input type="checkbox" checked=false id="buttonMode"></p>',
							'<p>Collidable(inactive): <input type="checkbox" checked=true id="collidableInactive"></p>',
							'<p>Collidable(active): <input type="checkbox" checked=true id="collidableActive"></p>',
						],
						draggable: true,
						onConfirm() {
							dialog.hide();
							options.label = $('.dialog#oc_3dm_export input#label').val();
							options.lightLevel = parseInt($('.dialog#oc_3dm_export input#lightLevel').val());
							options.emitRedstone = parseInt($('.dialog#oc_3dm_export input#emitRedstone').val());
							options.buttonMode = $('.dialog#oc_3dm_export input#buttonMode').is(':checked');
							options.collidableInactive = $('.dialog#oc_3dm_export input#collidableInactive').is(':checked');
							options.collidableActive = $('.dialog#oc_3dm_export input#collidableActive').is(':checked');
							codec.export();
						}
					});
					dialog.show();
				}
			})

			MenuBar.addAction(export_action, 'file.export')
		},

		onunload() {
			export_action.delete();
		}
	});

})()
